package com.ycy.query;

import com.ycy.BaseTest;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.variable.api.history.HistoricVariableInstance;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class QueryDemo extends BaseTest {

  @Test
  public void create() {
    //定义和创建一个流程实例

    RepositoryService repositoryService = processEngine.getRepositoryService();
    Deployment deployment = repositoryService.createDeployment()
            .addClasspathResource("query/abcdef.bpmn20.xml")
            .deploy();

    RuntimeService runtimeService = processEngine.getRuntimeService();
    HashMap<String, Object> map = new HashMap<>();
    String[] v = {"shareniu1", "shareniu2", "shareniu3", "shareniu4"};
    map.put("per", "bbb");
    map.put("money", "1111");
    map.put("assigneeList", Arrays.asList(v));
    ProcessInstance pi = runtimeService.startProcessInstanceByKey("abcdef", map);
    System.out.println("==> 流程实例启动 实例id :" + pi.getProcessInstanceId());

  }

  /**
   * 11.1.2. 历史变量实例查询
   * HistoricVariableInstance保存流程变量或任务变量的最新值。
   */
  @Test
  public void test1() {
    HistoryService historyService = processEngine.getHistoryService();
    List<HistoricVariableInstance> list = historyService.createHistoricVariableInstanceQuery()
            .processInstanceId("4")
            .list();
    System.out.println("==> 变量个数：" + list.size());
    for (HistoricVariableInstance historicVariableInstance : list) {
      System.out.println(historicVariableInstance.getProcessInstanceId() + " : " + historicVariableInstance.getTaskId());
      System.out.println(historicVariableInstance);
    }

  }

  /**
   * 11. 历史
   * HistoricActivityInstance保存活动（流程中的节点）的一次执行的信息
   */
  @Test
  public void query111111() {
    HistoryService historyService = processEngine.getHistoryService();
    List<HistoricActivityInstance> activities =
            historyService.createHistoricActivityInstanceQuery()
                    .processInstanceId("4")
                    .orderByHistoricActivityInstanceEndTime().asc()
                    .list();
    for (HistoricActivityInstance activity : activities) {
      System.out.println(activity);
    }
  }

}
